﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Windows.Forms;

namespace RefactoringTechniques
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string[] items = new string[6] { string.Empty, "Bloaters", "Object-Orientation Abusers", "Change Preventers", "Dispensables", "Couplers" };
            AddItems(CodeSmellGroups.Items, items);
        }

        private void HandleSelectionChange(object sender, EventArgs e)
        {
            ComboBox sorz = (ComboBox)sender;
            if (sorz.Equals(CodeSmellGroups))
            {
                if(CodeSmellGroups.Text == "Bloaters")
                {
                    CodeSmells.Items.Clear();
                    Refactorings.Items.Clear();
                    CodeSmells.Text = string.Empty;
                    string[] items = new string[6] { string.Empty, "Long Method", "Large Class", "Primitive Obsession", "Long Parameter List", "DataClumps" };
                    AddItems(CodeSmells.Items, items);
                }
                else if(CodeSmellGroups.Text == "Object-Orientation Abusers")
                {
                    CodeSmells.Items.Clear();
                    Refactorings.Items.Clear();
                    CodeSmells.Text = string.Empty;
                    string[] items = new string[5] { string.Empty, "Switch Statements", "Temporary Field", "Refused Request", "Alternative Class with Different Interface" };
                    AddItems(CodeSmells.Items, items);
                }
                else if(CodeSmellGroups.Text == "Change Preventers")
                {
                    CodeSmells.Items.Clear();
                    Refactorings.Items.Clear();
                    CodeSmells.Text = string.Empty;
                    string[] items = new string[4] { string.Empty, "Divergent Change", "Shotgun Surgery", "Parallel Inheritance Hierarchies" };
                    AddItems(CodeSmells.Items, items);
                }
                else if(CodeSmellGroups.Text == "Dispensables")
                {
                    CodeSmells.Items.Clear();
                    Refactorings.Items.Clear();
                    CodeSmells.Text = string.Empty;
                    string[] items = new string[6] { string.Empty, "Lazy Class", "Data Class", "Duplicate Code", "Dead Code", "Speculative Generality" };
                    AddItems(CodeSmells.Items, items);
                }
                else if(CodeSmellGroups.Text == "Couplers")
                {
                    CodeSmells.Items.Clear();
                    Refactorings.Items.Clear();
                    CodeSmells.Text = string.Empty;
                    string[] items = new string[5] { string.Empty, "Feature Envy", "Inappropriate Intimacy", "Message Chains", "Middle Man" };
                    AddItems(CodeSmells.Items, items);
                }
            }
            else if (sorz.Equals(CodeSmells))
            {
                if (CodeSmellGroups.Text == "Bloaters")
                {
                    if (CodeSmells.Text == "Long Method")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[6] { "Extract Method", "Introduce Parameter Object", "Decompose Conditional If", "Preserve Whole Object", "Replace Method with Method Object", "Replace Temp with Query" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Large Class")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[4] { "Extract Class", "Extract Subclass", "Extract Interface", "Replace Data Value with Object" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Primitive Obsession")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[6] { "Replace Data Value with Objects", "Introduce Parameter Object", "Extract Class", "Replace Type Code with Class", "Replace Type Code with Subclass", "Replace Array with Object" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Long Parameter List")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[3] { "Replace Parameter with Method", "Introduce Parameter Object", "Preserve Whole Object" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "DataClumps")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[3] { "Extract Class", "Preserve Whole Object", "Introduce Parameter Object" };
                        AddItems(Refactorings.Items, items);
                    }
                }
                else if (CodeSmellGroups.Text == "Object-Orientation Abusers")
                {
                    if (CodeSmells.Text == "Switch Statements")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[4] { "Replace Conditional with Polymorphism", "Replace Type Code with Subclass", "Replace Type Code with State/Strategy", "Replace Parameter with Explicit Methods" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Temporary Field")
                    {
                        Refactorings.Items.Clear();
                        Refactorings.Items.Add("Extract Class");
                    }
                    else if (CodeSmells.Text == "Refused Request")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[3] { "Push Down Field", "Push Down Method", "Replace Inheritance with Delegation" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Alternative Class with Different Interface")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[2] { "Rename Method", "Move Method" };
                        AddItems(Refactorings.Items, items);
                    }
                }
                else if (CodeSmellGroups.Text == "Change Preventers")
                {
                    if (CodeSmells.Text == "Divergent Change")
                    {
                        Refactorings.Items.Clear();
                        Refactorings.Items.Add("Extract Class");
                    }
                    else if (CodeSmells.Text == "Shotgun Surgery")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[3] { "Move Method", "Move Field", "Inline Class" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Parallel Inheritance Hierarchies")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[2] { "Move Method", "Move Field" };
                        AddItems(Refactorings.Items, items);
                    }
                }
                else if (CodeSmellGroups.Text == "Dispensables")
                {
                    if (CodeSmells.Text == "Lazy Class")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[3] { "Collapse Hierarchy", "Inline Class", "Inline Singleton" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Data Class")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[3] { "Move Method", "Encapsulate Field", "Encapsulate Collection" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Duplicate Code")
                    {
                        Refactorings.Items.Clear();
                        Refactorings.Items.Add("Extract Method");
                    }
                    else if (CodeSmells.Text == "Dead Code")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[2] { "Source Control", "Delete It" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Speculative Generality")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[4] { "Collapse Hierarchy", "Rename Method", "Remove Parameter", "Inline Class" };
                        AddItems(Refactorings.Items, items);
                    }
                }
                else if (CodeSmellGroups.Text == "Couplers")
                {
                    if (CodeSmells.Text == "Feature Envy")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[] { "Extract Method", "Move Method", "Move Field" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Inappropriate Intimacy")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[] { "Move Method", "Move Field", "Change Bidirectional Association to Unidirectional Association", "Extract Class", "Extract Delegate", "Replace Inheritance with Delegation" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Message Chains")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[] { "Hide Delegate", "Extract Method", "Move Method" };
                        AddItems(Refactorings.Items, items);
                    }
                    else if (CodeSmells.Text == "Middle Man")
                    {
                        Refactorings.Items.Clear();
                        string[] items = new string[3] { "Remove Middle Man", "Inline Method", "Replace Delegation with Inheritance" };
                        AddItems(Refactorings.Items, items);
                    }
                }
            }
        }

        public void AddItems(IList listControl, string[] items)
        {
            if (listControl == null)
                return;

            if (items == null)
                return;

            foreach (string item in items)
            {
                listControl.Add(item);
            }
        }
    }
}
